package com.ly.emp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.emp.bean.vo.HubTeamXsSetVO;
import com.ly.emp.entity.HubTeamXsSet;
import com.ly.emp.mapper.HubTeamXsSetMapper;
import com.ly.emp.service.IHubTeamXsSetService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 工作系数-impl
 *
 * @author luoyun
 * @Date: 2025-05-16
 */
@Service
public class HubTeamXsSetServiceImpl extends ServiceImpl<HubTeamXsSetMapper, HubTeamXsSet> implements IHubTeamXsSetService {

    @Override
    public R getId(String id) {
        HubTeamXsSet entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(HubTeamXsSet entity) {
        List<HubTeamXsSet> list = this.list(
                new LambdaQueryWrapper<HubTeamXsSet>()
                        .eq(StrUtil.isNotEmpty(entity.getOrgCode()), HubTeamXsSet::getOrgCode, entity.getOrgCode())
                        .like(StrUtil.isNotBlank(entity.getXsName()), HubTeamXsSet::getXsName, entity.getXsName())
                        .orderByAsc(HubTeamXsSet::getOrgCode)
                        .orderByAsc(HubTeamXsSet::getDisplayOrder)
        );
        return R.ok(list);
    }

    @Override
    public R getPage(HubTeamXsSet entity, Page<HubTeamXsSet> page) {
        IPage<HubTeamXsSet> pageList = this.page(
                page,
                new LambdaQueryWrapper<HubTeamXsSet>()
                        .eq(StrUtil.isNotEmpty(entity.getOrgCode()), HubTeamXsSet::getOrgCode, entity.getOrgCode())
                        .like(StrUtil.isNotBlank(entity.getXsName()), HubTeamXsSet::getXsName, entity.getXsName())
                        .orderByAsc(HubTeamXsSet::getOrgCode)
                        .orderByAsc(HubTeamXsSet::getDisplayOrder)
        );
        return R.ok(pageList);
    }

    @Override
    public R add(HubTeamXsSet entity) {
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(HubTeamXsSet entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<HubTeamXsSet> dto) {
        List<HubTeamXsSet> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }

    @Override
    public R<List<HubTeamXsSetVO>> findList(HubTeamXsSet entity) {
        String orgCode = entity.getOrgCode();
        if(StrUtil.isEmpty(orgCode)){
            return R.ok();
        }
        List<HubTeamXsSetVO> vos = new ArrayList<>();
        List<HubTeamXsSet> list = this.list(
                new LambdaQueryWrapper<HubTeamXsSet>()
                        .eq(HubTeamXsSet::getOrgCode, entity.getOrgCode())
                        .orderByAsc(HubTeamXsSet::getDisplayOrder)
        );
        list.stream().forEach(n -> vos.add(BeanUtil.copyProperties(n, HubTeamXsSetVO.class)));
        return R.ok(vos);
    }
}
